package cn.diancan.service.impl;


import java.util.ArrayList;
import java.util.List;

import javax.annotation.Resource;

import org.apache.commons.beanutils.BeanUtils;
import org.springframework.stereotype.Component;

import cn.diancan.dao.MyBaseDao;
import cn.diancan.exception.OutMsgException;
import cn.diancan.model.SysAuthority;
import cn.diancan.model.types.CommonType;
import cn.diancan.service.SysAuthorityService;
import cn.diancan.vo.SysAuthorityVo;
import cn.diancan.vo.other.ResultVoUtil;

@Component("sysAuthorityService")
public class SysAuthorityServiceImpl   implements SysAuthorityService {
    
    @Resource(name="MyBaseDao")
    private MyBaseDao<SysAuthority> sysAuthorityDao;
    

    @Override
    public boolean saveAuthority(SysAuthority bean) throws OutMsgException,Exception {
	if (null != bean) {
	    if (bean.getName().trim() == "") {
		throw new OutMsgException("参数错误");
	    }// 如果存在这个名称的权限。就只是激活这个权限而已
	    if (null != findByName(bean.getName())) {
		bean = findByName(bean.getName());
		bean.setIsDelete(CommonType.Delete.NO);
		sysAuthorityDao.update(bean);
		return true;
	    }
	    sysAuthorityDao.save(bean);
	    bean.setIndexNo(Integer.valueOf(bean.getId().toString()));// 初始化排序号，跟id一致
	}
	return true;
    }

    @Override
    public SysAuthority findByName(String name) throws OutMsgException  {
	if ("".equals(name.trim())) {
	    throw new OutMsgException("参数错误");
	}
	return sysAuthorityDao.get("FROM SysAuthority where name=?",
		new Object[] { name });
    }

    @Override
    public List<SysAuthority> findAll() {
	return sysAuthorityDao.find("FROM SysAuthority WHERE isDelete=? ORDER BY indexNo ASC",new Object[]{CommonType.Delete.NO});
    }

    @Override
    public List<SysAuthority> findTopList() {
	return sysAuthorityDao.find("FROM SysAuthority WHERE parent is null AND isDelete=? ORDER BY indexNo ASC",new Object[]{CommonType.Delete.NO});
    }

    @Override
    public List<SysAuthority> findByIds(String ids)
	    throws NumberFormatException, OutMsgException {
	String[] idString = ids.split(",");
	List<SysAuthority> list = new ArrayList<SysAuthority>();
	for (String id : idString) {
	    if (null != id && !"".equals(id)) {
		list.add(findById(Long.valueOf(id)));
	    }
	}
	return list;

    }

    @Override
    public SysAuthority findById(Long id) throws OutMsgException {
	if (null == id) {
	    throw new OutMsgException("参数错误");
	}
	SysAuthority bean = sysAuthorityDao.get(
		"FROM SysAuthority WHERE  id=?", new Object[] { id });
	if (null == bean) {
	    throw new OutMsgException("信息不存在");
	}
	return bean;
    }

    @Override
    public List<String> findAllUrl() {
	List<String> list = new ArrayList<String>();
	for (SysAuthority bean : findAll()) {
	    if (bean.getUrl() != null) {
		list.add(bean.getUrl());
	    }
	}
	return list;
    }

    @Override
    public List<SysAuthorityVo> findTopListBySql() {
	String hql = "SELECT id,url,name,parent_id,creationDate,modificationDate,indexNo FROM SysAuthority "
		+ "WHERE parent_id is null " + "ORDER BY indexNo";
	List<Object[]> lsObj = sysAuthorityDao.findSql(hql, new Object[] {}, 1,10);
	return ResultVoUtil.object2Model(SysAuthorityVo.class, lsObj, hql);
    }

    @Override
    public List<SysAuthorityVo> findTopChild(Long id) throws OutMsgException {
	SysAuthority bAuthority = findById(id);
	List<SysAuthorityVo> list = new ArrayList<SysAuthorityVo>();
	for (SysAuthority bean : bAuthority.getChildren()) {
	    if (null != bean && bean.getIsDelete() == 0) {
		SysAuthorityVo nAuthorityVo = new SysAuthorityVo();
		try {
		    BeanUtils.copyProperties(nAuthorityVo, bean);
		} catch (Exception e) { 
		    e.printStackTrace();
		}
		list.add(nAuthorityVo);
	    }
	}
	return list;
    }

    @Override
    public Boolean delete(Long id) throws OutMsgException,Exception {
	SysAuthority nAuthorit = findById(id);
	nAuthorit.setParent(null);
	sysAuthorityDao.update(nAuthorit);
	sysAuthorityDao.delete(nAuthorit);
	return true;
    }

    @Override
    public boolean updateIndex(boolean b, Long pId, Long id) throws OutMsgException,Exception {
	String hql = "FROM SysAuthority WHERE 1=1";
	SysAuthority m = findById(id);
	SysAuthority otherModel = null;
	List<Object> param = new ArrayList<Object>();
	if (null == pId) {
	    hql += " AND parent IS NULL ";
	} else {
	    hql += " AND parent.id=?";
	    param.add(pId);
	}
	if (b) {// 上移
	    hql += " AND indexNo<?";
	    hql += " ORDER BY indexNo DESC";
	}
	// 下移
	else {
	    hql += " AND indexNo>?";
	    hql += " ORDER BY indexNo";
	}
	param.add(m.getIndexNo());

	List<SysAuthority> list = sysAuthorityDao.find(hql, param, 0, 1);
	if (sysAuthorityDao.find(hql, param, 0, 1).size() > 0) {
	    otherModel = list.get(0);
	    int temp = otherModel.getIndexNo();
	    otherModel.setIndexNo(m.getIndexNo());
	    m.setIndexNo(temp);
	}
	sysAuthorityDao.update(otherModel);
	sysAuthorityDao.update(m);
	return true;
    }
}
